#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <stdio.h>
#include <sys/time.h>
#include "app_platform/cntl_info.h"
#include "app_platform/cgi_stub_cntl.h"
#include "../include/giscoreao_stub4web_allinone.h"
#include "erp/gis/po/shiptypeareagischeckpo_gis.h"	
#include "erp/gis/po/mergedcoordinatespo_gis.h"	
#include <map>	
#include <vector>
#include "erp/comm/erp_cache_client.h"
#include "erp/gis/ddo/shiptypearealocusdolist_gis.h"

using namespace erp::comm;
using namespace erp::gis::ao;
std::string strTmemSvrName ="erp_gis_tmem";
uint32_t dwTmemBid;
const std::map<TmemCacheItilEnum, uint32_t> mapTmemItil;
int Init()
{
	int iRet = TMEM_CACHE_OP->Initialize(
		strTmemSvrName,
		dwTmemBid,
		mapTmemItil
		);

    if(iRet != 0)
    {
        std::cout << "init Tmem Failed." << std::endl;
    }
}

std::string GetTmemKey( 
     uint32_t dwShipType,
     uint32_t dwDestGbId
     )
{
    std::stringstream oss;
    oss << "gis_shiparea_locus_"
        << dwShipType
        << "_"
        << dwDestGbId;

    return oss.str();
}

void GetCache(const std::string& strKey)
{
    uint32_t dwCacheExpireTimeGet;
    erp::gis::ddo::CShipTypeAreaLocusDoList oShipTypeAreaLocusDoList; 
    int iRetCode = TMEM_CACHE_OP->GetSimpleData(strKey,oShipTypeAreaLocusDoList,dwCacheExpireTimeGet);
    if(iRetCode != 0)
    {
        std::cout << "Tmem GetData Failed. key: " << strKey << std::endl;
        return;
    }
    std::cout << "GetData For Key: " << strKey << " success! Record Number: " << oShipTypeAreaLocusDoList.GetShipTypeAreaLocusDo().size() <<std::endl; 
}

void DelCache(const std::string& strKey)
{
    int iCacheRetCode = TMEM_CACHE_OP->DelData(strKey, dwTmemBid);
    if(iCacheRetCode != 0)
    {
        std::cout << "Tmem DelData Failed. key: " << strKey << std::endl;
        return;
    }

    std::cout << "DelData For Key: " << strKey << " success!  " <<std::endl; 
}
void PrintUsage()
{
    std::cout << "===================================================================" << std::endl;
    std::cout << "Useage:" << std::endl;
    std::cout << "./gis_cache_tool -o operator -a areaid -t shiptype -b bid" << std::endl;
    std::cout << "operator:  0:get 1:del" << std::endl;
    std::cout << "bid: dev:100190 beta:100191 " << std::endl;
    std::cout << "Example:./gis_cache_tool  " << std::endl;
    std::cout << "===================================================================" << std::endl;
}
int main(int argc, char** argv)
{

    if(argc != 9)
    {
        PrintUsage();
        return 1;
    }

    uint32_t dwShipType = 0;
    uint32_t dwAreaId = 0;
    uint32_t is_get = 1;
    for (int i = 1; i < argc; i+=2)
    {
        char * op = argv[i];
        switch(op[1])
        {
            case 'o':
                is_get = std::strtoul(argv[i+1],0,10);
                break;
            case 'a':
                dwAreaId = std::strtoul(argv[i+1],0,10);
                break;
            case 't':
                dwShipType = std::strtoul(argv[i+1],0,10);
                break;
            case 'b':
                dwTmemBid = std::strtoul(argv[i+1],0,10);
                break;
            default:
                break;
        }
    }
    if(Init() != 0)
    {
        return 1;
    }

    std::string strKey = GetTmemKey(dwShipType,dwAreaId);
    if(is_get == 1)
    {
        GetCache(strKey);

    }
    else
    {
        DelCache(strKey);
    }
    return 0;
}
